package com.asnike.util{
	
	
	public class ASINIParser{
		
		static public function parse( $data:String ):Object{
			var i:int, j:int, temp:*, result:Object, key:String;
			
			temp = $data.replace( /[\n]/g, '' );
			temp = temp.split( /\r/ );
			
			result = {};
			for( i = 0, j = temp.length ; i < j ; ++i ){
				if( isKeyValue( temp[i] ) ){
					key = getKeyValue( temp[i] );
					if( isSubKeyValue( temp[i + 1] ) ){
						result[key] = {};
					}else{
						result[key] = [];
					}
				}else if( isEmptyValue( temp[i] ) ){
					
				}else if( isSubKeyValue( temp[i] ) ){
					result[key][getSubKeyValue( temp[i] )] = getSubDataValue( temp[i] );
				}else{
					result[key][result[key].length] = temp[i];
				}
			}
			
			return result;
		}
		static private function isKeyValue( $str:String ):Boolean{
			return ( $str && $str.indexOf( '[' ) > -1 );
		}
		static private function isSubKeyValue( $str:String ):Boolean{
			return ( $str && $str.indexOf( '=' ) > -1 );
		}
		static private function isEmptyValue( $str:String ):Boolean{
			return $str === '';
		}
		static private function getKeyValue( $str:String ):String{
			return $str.substring( $str.indexOf( '[' ) + 1, $str.indexOf( ']' ) );
		}
		static private function getSubKeyValue( $str:String ):String{
			return $str.substring( 0, $str.indexOf( '=' ) );
		}
		static private function getSubDataValue( $str:String ):String{
			return $str.substr( $str.indexOf( '=' ) + 1 );		
		}
		
		public function ASINIParser(){
		}
	}
}